<?php

class manageSitemap {
	var $table='bj_audio';
	var $products=array();
	var $content='';
	var $tblwidth="80%";
	var $page=0;
	var $nrpages=0;
	var $nrows=0;
	var $islist=0;
	
	function manageSitemap() {
		if(isset($_GET['action']) ) {
			$data=utils::gpc_check($_GET);
			switch($_GET['action']) {
				case 'submit' :
					$this->submitSitemap();
					break;
				case 'generate' :
					$this->regenerateSitemap();;
					break;
				default :
					$this->listItems();
			}
		} else {
			$this->listItems();
		}
		$this->tohtml();
	}

  function regenerateSitemap() {
  	define('_CRLF_' ,"\n");
  	
  	$xml = '<?xml version="1.0" encoding="UTF-8"?>' . _CRLF_;
  	$xml .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">' . _CRLF_;
  
  	$res = mysql_query("SELECT MIN(item_views), MAX(item_views) FROM traffic_stats") or die(mysql_error());
  	$lin = mysql_fetch_row($res);
  	mysql_free_result($res);
  	define('__max__', $lin[1]);
  	define('__min__', $lin[0]);
  	
  	$total_urls = 0;
  	$res = mysql_query("SELECT ks.item_views, k.file_title FROM bj_audio AS k LEFT JOIN traffic_stats AS ks ON k.id_file=ks.item_id ORDER BY item_views DESC") or die(mysql_error());
  	if ($res !== FALSE && mysql_num_rows($res)) {
  		$total_urls = mysql_num_rows($res);
  		while( $lin = mysql_fetch_assoc($res) ) {
  			$priority = intval($lin['item_views']) / __max__;
  			$priority = $priority < .1 ? .1 : number_format($priority, 1, '.', '');
  			
  			$xml .= "\t<url>" . _CRLF_;
            $xml .= "\t\t<loc>" . utils::create_href($lin['file_title']) . "</loc>" . _CRLF_;
  			$xml .= "\t\t<lastmod>" . date('Y-m-d') . '</lastmod>' . _CRLF_;
  			$xml .= "\t\t<changefreq>daily</changefreq>" . _CRLF_;
  			$xml .= "\t\t<priority>" . $priority . '</priority>' . _CRLF_;
  			$xml .= "\t</url>" . _CRLF_;
  		}
  		
  	}
  
  	$xml .= '</urlset>';
  	
  	$xml = mb_convert_encoding( $xml, "UTF-8");
  
  	$hd = fopen ('../sitemap.xml' , 'wb');
  	fwrite($hd, $xml, strlen($xml));
  	fclose($hd);
    $this->content = <<<AAA
            <tr>
                <td height="30" colspan="2">The sitemap was successfully updated. Total number of links: {$total_urls}</td>
            </tr>
AAA;
  }
  
  
  function submitSitemap() {
    $hd = curl_init();
    curl_setopt ($hd, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
    curl_setopt ($hd, CURLOPT_REFERER, 'http://www.televisiontunes.com/');
    curl_setopt ($hd, CURLOPT_URL, 'http://www.google.com/webmasters/sitemaps/ping?sitemap=' . htmlspecialchars(utf8_encode('http://www.televisiontunes.com/sitemap.xml')));
    curl_setopt ($hd, CURLOPT_HEADER, 1);
    curl_setopt ($hd, CURLOPT_POST, 0); 
    curl_setopt ($hd, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($hd, CURLOPT_COOKIEFILE, 0);
    curl_setopt ($hd, CURLOPT_FOLLOWLOCATION, 1);
    $resp = curl_exec($hd);
    curl_close($hd);
    $this->content = <<<AAA
            <tr>
                <td height="30" colspan="2">The sitemap was successfully resubmitted to google</td>
            </tr>
AAA;
  }
  

		

	function listItems() {
		$this->content=<<<AAA
            <tr>
                <td height="30" colspan="2">&nbsp;</td>
            </tr>
				<tr>
                <td align="center"><input type="button" value="Regenerate sitemap" onclick="document.location.href='./google_sitemap.php?action=generate'"></td>
                <td align="center"><input type="button" value="Resubmit sitemap" onclick="document.location.href='./google_sitemap.php?action=submit'"></td>
				</tr>
AAA;
	}
	
	function tohtml() {
		$content = '<table border="1" cellpadding="3" cellspacing="1" width="'.$this->tblwidth.'" style="border-collapse:collapse;" align="center">';
		$content.=$this->content;	
		$content.='</table>';
		echo $content;
	}	
	
	
} 
?>
